<template>
  <div class="family-medical">
    <div class="header">
      <h2>健康档案</h2>
      <p>查看我家老人的健康记录信息</p>
    </div>

    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>健康记录列表</span>
          <el-button type="primary" @click="loadMedicalRecords" :loading="loading">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </template>

      <el-table :data="medicalRecords" style="width: 100%" v-loading="loading">
        <el-table-column prop="recordDate" label="记录日期" width="120">
          <template #default="{ row }">
            {{ formatDate(row.recordDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="elderName" label="老人姓名" width="100"></el-table-column>
        <el-table-column prop="bloodPressure" label="血压" width="120">
          <template #default="{ row }">
            <span v-if="row.bloodPressure">{{ row.bloodPressure }}</span>
            <span v-else class="text-muted">未记录</span>
          </template>
        </el-table-column>
        <el-table-column prop="heartRate" label="心率" width="80">
          <template #default="{ row }">
            <span v-if="row.heartRate">{{ row.heartRate }}次/分</span>
            <span v-else class="text-muted">未记录</span>
          </template>
        </el-table-column>
        <el-table-column prop="temperature" label="体温" width="80">
          <template #default="{ row }">
            <span v-if="row.temperature">{{ row.temperature }}°C</span>
            <span v-else class="text-muted">未记录</span>
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="体重" width="80">
          <template #default="{ row }">
            <span v-if="row.weight">{{ row.weight }}kg</span>
            <span v-else class="text-muted">未记录</span>
          </template>
        </el-table-column>
        <el-table-column prop="healthStatus" label="健康状况" width="120">
          <template #default="{ row }">
            <el-tag :type="getHealthStatusTag(row.healthStatus)">
              {{ row.healthStatus || '正常' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="notes" label="备注" min-width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewDetail(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailVisible"
      title="健康记录详情"
      width="600px"
    >
      <el-descriptions :column="2" border v-if="currentRecord">
        <el-descriptions-item label="老人姓名">{{ currentRecord.elderName }}</el-descriptions-item>
        <el-descriptions-item label="记录日期">{{ formatDate(currentRecord.recordDate) }}</el-descriptions-item>
        <el-descriptions-item label="血压">{{ currentRecord.bloodPressure || '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="心率">{{ currentRecord.heartRate ? currentRecord.heartRate + '次/分' : '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="体温">{{ currentRecord.temperature ? currentRecord.temperature + '°C' : '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="体重">{{ currentRecord.weight ? currentRecord.weight + 'kg' : '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="血糖">{{ currentRecord.bloodSugar ? currentRecord.bloodSugar + 'mmol/L' : '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="血氧">{{ currentRecord.bloodOxygen ? currentRecord.bloodOxygen + '%' : '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="健康状况" :span="2">
          <el-tag :type="getHealthStatusTag(currentRecord.healthStatus)">
            {{ currentRecord.healthStatus || '正常' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="症状描述" :span="2">{{ currentRecord.symptoms || '无' }}</el-descriptions-item>
        <el-descriptions-item label="用药情况" :span="2">{{ currentRecord.medication || '无' }}</el-descriptions-item>
        <el-descriptions-item label="护理建议" :span="2">{{ currentRecord.careAdvice || '无' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentRecord.notes || '无' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { medicalApi } from '../../api/medical'

const loading = ref(false)
const medicalRecords = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const detailVisible = ref(false)
const currentRecord = ref(null)

// 加载健康记录
const loadMedicalRecords = async () => {
  try {
    loading.value = true
    const response = await medicalApi.getFamilyMedicalRecords({
      page: currentPage.value,
      size: pageSize.value
    })
    if (response.code === 200) {
      medicalRecords.value = response.data.records || []
      total.value = response.data.total || 0
    } else {
      ElMessage.error(response.message || '加载失败')
    }
  } catch (error) {
    console.error('加载健康记录失败:', error)
    ElMessage.error('加载健康记录失败')
  } finally {
    loading.value = false
  }
}

// 查看详情
const viewDetail = (record) => {
  currentRecord.value = record
  detailVisible.value = true
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

// 获取健康状态标签类型
const getHealthStatusTag = (status) => {
  const statusMap = {
    '良好': 'success',
    '正常': 'info',
    '注意': 'warning',
    '异常': 'danger'
  }
  return statusMap[status] || 'info'
}

// 分页处理
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  currentPage.value = 1
  loadMedicalRecords()
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  loadMedicalRecords()
}

onMounted(() => {
  loadMedicalRecords()
})
</script>

<style scoped>
.family-medical {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  color: #303133;
}

.header p {
  margin: 5px 0 0 0;
  color: #909399;
  font-size: 14px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.text-muted {
  color: #c0c4cc;
  font-style: italic;
}
</style>
